<?php
class feed {
	var $xml;
	var $excel;
	var $csv;
	var $txt;
	var $site;
	var $media;
	var $data;
	var $mapping;
	public function __construct($website,$products){
		$this->mapping=array();
		$this->site="http://".$website."/";
		$this->media=$this->site."media/catalog/product";
		$this->xml="<?xml version='1.0' encoding='utf-8' ?>\n";	
		$this->excel='<html xmlns:o="urn:schemas-microsoft-com:office:office"
				xmlns:x="urn:schemas-microsoft-com:office:excel"
				xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]">
				<head>
				<meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
				<meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
				 <!--[if gte mso 9]><xml>
				 <x:ExcelWorkbook>
				 <x:ExcelWorksheets>
				 <x:ExcelWorksheet>
				 <x:Name></x:Name>
				 <x:WorksheetOptions>
				 	<x:DisplayGridlines/>
				</x:WorksheetOptions>
				</x:ExcelWorksheet>
				</x:ExcelWorksheets>
				</x:ExcelWorkbook>
				</xml><![endif]-->
				</head>
				 <body link=blue vlink=purple leftmargin=0 topmargin=0>
				 ';
		$this->txt="";
		$this->csv="";
		$this->data=array();
		if($products){
			$count=sizeof($products);
			for($i=0;$i<$count;$i++){
				if(isset($products[$i]['special_price']) || isset($products[$i]['price'])){
				if(isset($products[$i]['special_price'])){
					$price=$products[$i]['special_price'];
				}
				else{
					$price=$products[$i]['price'];
					}
				if($price==0){
					$price=$products[$i]['price'];
				}
				if($price!=0){
					$price=number_format($price,2,'.',' ');
					$this->data[]=array(
									'id'			=>		$products[$i]['entity_id'],
									'made_in'		=>		'CHINA',
									'name'			=>		$products[$i]['name'],
									'description'	=>		htmlFilter($products[$i]['short_description']),
									'product_url'	=>		$this->site.$products[$i]['url_path'],
									'image_url'		=>		$this->media.$products[$i]['image'],
									'sku'			=>		$products[$i]['sku']."_".$website,
									'price'			=>		$price,
									'weight'		=>		$products[$i]['weight'],								
									'condition'		=> 		'new',
									'is_store_have' =>		'Y'
									);						
					}
				
			}
		}
	}
}
		
	
	
	public function toTxt(){
		return	$this->toTxtOrCSV("\t");
		}
	
	public function toCsv(){
		return $this->toTxtOrCsv(",");
		}
	
	private function toTxtOrCSV($delimiter){
		 $count=sizeof($this->data);
		 $i=0;
	 	 if(is_array($this->mapping)){
			foreach($this->mapping as $key=>$value){
				if($i==0){
					$this->txt.=$key;
					}
				else{
					$this->txt.=$delimiter.$key;
					}
				$i++;
				}
		$this->txt.=chr(13);		
		
		foreach($this->data as $info){
			$i=0;
			foreach($this->mapping as $key=>$value){
					if(substr($value,0,6)!='CUSTOM'){
						$value=$info[$value];
						}
					else{
						$value=substr($value,7);
						}
						$this->txt.=($i==0)?"":$delimiter;					
						$this->txt.=$value;						
					$i++;
				}
			$this->txt.=chr(13);
			
		}
		return $this->txt;
	}
	}
	
	private function htmlFilter($str) {
	return preg_replace("@<(.*?)>@is","",$str);
	}
	
	
	public function toString(){
		return $this->data;
		}
}


?>